package com.hkfn.rfb.common.utils;

import com.alibaba.fastjson.JSON;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * JSON 工具
 */
public class JSONUtil {

    /**
     * JSONArray转变成BeanList
     * @param jsonArray
     * @param classOfT 类
     * @return BeanList
     */
    public static <T> List<T> jsonArrayToBeanList(JSONArray jsonArray, Class<T> classOfT){
        return JSON.parseArray(jsonArray.toString(), classOfT);
    }


    /**
     * JSONObject转变成Bean
     * @param content
     * @param classOfT 类
     * @return Bean
     */
    public static <T> T jsonObjectToBean(String content, Class<T> classOfT){
        return JSON.parseObject(content, classOfT);
    }

    /**
     * Bean转变成JSON
     * @param t
     * @param <T>
     * @return
     */
    public static <T> String beanToJson(T t){
        return JSON.toJSONString(t);
    }

    /**
     * JSONObject转HashMap
     * @param jsonStr
     * @return
     */
    public static HashMap<String, String> jsonObjectToMap(String jsonStr){
        if (StringUtils.isEmpty(jsonStr)) {
            return new HashMap<String, String>();
        }
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(jsonStr);
        } catch (JSONException e) {
            return new HashMap<String, String>();
        }
        HashMap<String, String> map = new HashMap<String, String>();
        Iterator iterator = jsonObject.keys();
        while(iterator.hasNext()){
            String key = (String) iterator.next();
            try {
                String value = jsonObject.getString(key);
                if (StringUtils.isEmpty(value)) {
                    continue;
                }
                map.put(key, value);
            } catch (JSONException e) {
                return new HashMap<String, String>();
            }
        }
        return map;
    }

    /**
     * JSONObject转HashMap
     * @param jsonObject
     * @return
     */
    public static HashMap<String, String> jsonObjectToMap(JSONObject jsonObject){
        if (null == jsonObject){
            jsonObject = new JSONObject();
        }
        HashMap<String, String> map = new HashMap<String, String>();
        Iterator iterator = jsonObject.keys();
        while(iterator.hasNext()){
            String key = (String) iterator.next();
            try {
                String value = jsonObject.getString(key);
                if (StringUtils.isEmpty(value)) {
                    continue;
                }
                map.put(key, value);
            } catch (JSONException e) {
                return new HashMap<String, String>();
            }
        }
        return map;
    }

    /**
     * JSONObject转HashMap
     * @param jsonObject
     * @return
     */
    public static LinkedHashMap<String, String> jsonObjectToLinkMap(JSONObject jsonObject){
        if (null == jsonObject){
            jsonObject = new JSONObject();
        }
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        Iterator iterator = jsonObject.keys();
        while(iterator.hasNext()){
            String key = (String) iterator.next();
            try {
                String value = jsonObject.getString(key);
                if (StringUtils.isEmpty(value)) {
                    continue;
                }
                map.put(key, value);
            } catch (JSONException e) {
                return new LinkedHashMap<String, String>();
            }
        }
        return map;
    }

    /**
     * 从JSON中取得key的值
     * @param jsonStr
     * @param key
     * @return
     */
    public static String getWantedValueByJsonStr(String jsonStr, String key) {
        HashMap<String, String> map = jsonObjectToMap(jsonStr);
        if (null != map && map.size() > 0) {
            return map.get(key);
        } else {
            return "";
        }
    }
    /**
     * JSONObject转HashMap
     * @param jsonStr
     * @return
     */
    public static HashMap<String, Object> jsonObjectToMapObject(String jsonStr){
        if (StringUtils.isEmpty(jsonStr)) {
            return new HashMap<String, Object>();
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(jsonStr);
        } catch (JSONException e) {
            return new HashMap<String, Object>();
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        Iterator iterator = jsonObject.keys();
        while(iterator.hasNext()){
            String key = (String) iterator.next();
            try {
                String value = jsonObject.getString(key);
                if (StringUtils.isEmpty(value)) {
                    continue;
                }
                map.put(key, value);
            } catch (JSONException e) {
                return new HashMap<String, Object>();
            }
        }
        return map;
    }

    public static String mapToJSonStr(Map<String, Object> map) {
        if (null == map) {
            return null;
        }

        boolean isFirst = true;
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        Iterator it = map.keySet().iterator();
        while(it.hasNext()){
            if (isFirst) {
                isFirst = false;
            } else {
                sb.append(",");
            }
            String key = it.next().toString();
            sb.append("\"");
            sb.append(key);
            sb.append("\"");
            sb.append(":");
            sb.append("\"");
            sb.append(map.get(key));
            sb.append("\"");
        }
        sb.append("}");
        return sb.toString();
    }

    public static JSONObject mapToJSon(Map<String, String> map) {
        if (null == map) {
            return null;
        }

        boolean isFirst = true;
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        Iterator it = map.keySet().iterator();
        while(it.hasNext()){
            if (isFirst) {
                isFirst = false;
            } else {
                sb.append(",");
            }
            String key = it.next().toString();
            sb.append("\"");
            sb.append(key);
            sb.append("\"");
            sb.append(":");
            sb.append("\"");
            sb.append(map.get(key));
            sb.append("\"");
        }
        sb.append("}");
        try {
            return new JSONObject(sb.toString());
        } catch (Exception e) {
            return  new JSONObject();
        }
    }

    public static String mapObjectToJSonStr(Map<String, Object> map, String decollator) {
        if (null == map) {
            return null;
        }

        boolean isFirst = true;
        StringBuilder sb = new StringBuilder();
        Iterator it = map.keySet().iterator();
        while(it.hasNext()){
            if (isFirst) {
                isFirst = false;
            } else {
                sb.append(decollator);
            }
            String key = it.next().toString();
            sb.append(key);
            sb.append("=");
            sb.append(map.get(key));
        }
        return sb.toString();
    }
}
